package yuangong.service;

import yuangong.Register;
import yuangong.Utils;
import yuangong.dao.BaseIndexDao;
import yuangong.dao.UserDao;
import yuangong.pojo.User;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class UserService {
    private final UserDao userDao;
    public UserService(){
        userDao = new UserDao();
    }
    public int add(HashMap<String,String> userMap){
        if (!Utils.isLegalId(userMap.get("id"))) {
            return -2;
        }
        // 判断id是否已存在，是则直接返回
        User userInDb = userDao.show(userMap.get("id"));
        if(userInDb!=null){
            return -1;
        }
        // 将插入的key value映射到user对象上
        final User user = new User();
        Method[] methods = user.getClass().getMethods();
        Arrays.stream(methods).forEach(method->{
            userMap.forEach((k,v)-> {
                if (("set" + k).equals(method.getName().toLowerCase())) {
                        Register.get(k).add(k,v);
                    try {
                        method.invoke(user,v);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        });
        this.userDao.add(user);
        return 0;
    }
    public void mod(String id, String key, String value) throws InvocationTargetException, IllegalAccessException {
        User user = userDao.show(id);
        if(user==null){
            return;
        }
        Method[] methods = user.getClass().getMethods();
        for(Method method:methods){
            if(method.getName().toLowerCase().equals("set"+key)){
                method.invoke(user,value);
            }
        }
        userDao.add(user);
        Register.get(key).add(id,value);
    }
    public void delete(String id){
        User user=userDao.show(id);
        Register.getAllDao().forEach(dao->{
            dao.delete(id,user.);
        });
        userDao.delete(id);
    }
    public User show(String key,String value){
        if("id".equals(key)){
            return userDao.show(key);
        }else if("name".equals(key)){
            return userDao.show(nameIdDao.find(value).get(0));
        }
        return null;
    }

    private void getSet(BaseIndexDao dao,HashSet<String>idSet,String value){
        ArrayList<String> outs =dao.find(value);
        if(idSet.isEmpty()){
            idSet.addAll(outs);
        }else{
            idSet.retainAll(outs);
        }
    }
    public Collection<User> showAll(HashMap<String,String> map){
        ArrayList<User>users=new ArrayList<>();
        if(map.containsKey("all")){
            for(String id:userDao.getIds()){
                users.add(userDao.show(id));
            }
        }
        HashSet<String>idSet=new HashSet<>();
        map.forEach((key,value)->{
            if(key.equals("department")){
                getSet(departmentIdDao,idSet,value);
            }else if(key.equals("name")){
                getSet(nameIdDao,idSet,value);
            }else if(key.equals("position")){
                getSet(positionIdDao,idSet,value);
            }
        });
        idSet.forEach((action)-> {
            users.add(userDao.show(action));
        });
        map.forEach((key,value)->{
            if(key.equals("order")){
                if(value.equals("date")){
                    users.sort(Comparator.comparing(User::getDate));
                }else if(value.equals("name")){
                    users.sort(Comparator.comparing(User::getName));
                }else if(value.equals("position")){
                    users.sort(Comparator.comparing(User::getPosition));
                }else if(value.equals("id")){
                    users.sort(Comparator.comparing(user->Integer.getInteger(user.getId())));
                }else if(value.equals("department")){
                    users.sort(Comparator.comparing(User::getDepartment));
                }
            }

            if(key.equals("desc_order")){
                if(value.equals("date")){
                    users.sort(Comparator.comparing(User::getDate).reversed());
                }else if(value.equals("name")){
                    users.sort(Comparator.comparing(User::getName).reversed());
                }else if(value.equals("position")){
                    users.sort(Comparator.comparing(User::getPosition).reversed());
                }else if(value.equals("id")){
                    users.sort(Comparator.comparing(user->Integer.getInteger(user.getId())));
                    users.sort(Collections.reverseOrder());
                }else if(value.equals("department")){
                    users.sort(Comparator.comparing(User::getDepartment));
                }
            }
        });
        return users;
    }
}
/*
* for test
add id:1 name:23 date:2002-12-02 position:98 department:87
add id:12 name:23 date:2002-12-12 position:98 department:87
*
* */